package com.apps.gomdiem.view;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import android.widget.EditText;

public class InputRequirement {
	public static final String MAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	private List<EditText[]> atLeastItems = new ArrayList<EditText[]>();
	private List<CharSequence> atLeastMsgs = new ArrayList<CharSequence>();

	private List<EditText[]> matchItems = new ArrayList<EditText[]>();
	private List<CharSequence> matchMsgs = new ArrayList<CharSequence>();

	private List<EditText> mailItems = new ArrayList<EditText>();
	private String mailErrorMsg;

	private List<EditText> blankItems = new ArrayList<EditText>();
	private String blankErrorMsg;

	/**
	 * 
	 * @param msg
	 * @param inputs
	 * @return This InputRequirement object to allow for chaining of calls to set methods
	 */
	public InputRequirement addAtLeast(CharSequence msg, EditText...inputs) {
		atLeastMsgs.add(msg);
		atLeastItems.add(inputs);
		return this;
	}

	/**
	 * 
	 * @param inputs
	 * @return This InputRequirement object to allow for chaining of calls to set methods
	 */
	public InputRequirement addMail(EditText...inputs) {
		mailItems.addAll(Arrays.asList(inputs));
		return this;
	}

	/**
	 * 
	 * @param inputs
	 * @return This InputRequirement object to allow for chaining of calls to set methods
	 */
	public InputRequirement addBlank(EditText...inputs) {
		blankItems.addAll(Arrays.asList(inputs));
		return this;
	}

	/**
	 * 
	 * @param msg
	 * @param inputs
	 * @return This InputRequirement object to allow for chaining of calls to set methods
	 */
	public InputRequirement addMissMatch(CharSequence msg, EditText...inputs) {
		matchMsgs.add(msg);
		matchItems.add(inputs);
		return this;
	}

	public boolean acceptRequirements() {
		// Check empty value
		for (EditText edit : blankItems)
			if (edit.getText().length() == 0) {
				edit.setError(blankErrorMsg);
				edit.requestFocus();
				return false;
			}

		// Check at least value
		for (int i = 0; i < atLeastItems.size(); i++) {
			boolean allEmpty = true;
			for (EditText edit : atLeastItems.get(i))
				if (edit.getText().length() > 0) {
					allEmpty = false;
					break;
				}
			if (allEmpty) {
				for (EditText edit : atLeastItems.get(i))
					edit.setError(atLeastMsgs.get(i));
				atLeastItems.get(i)[0].requestFocus();
				return false;
			}
		}

		// Check email valid
		for (EditText edit : mailItems)
			if (!Pattern.matches(MAIL_PATTERN, edit.getText())) {
				edit.setError(mailErrorMsg);
				edit.requestFocus();
				return false;
			}

		// Check miss match value
		for (int i = 0; i < matchItems.size(); i++) {
			boolean allMatch = true;
			String firstVal = null;
			for (EditText edit : matchItems.get(i)) {
				if (firstVal == null)
					firstVal = edit.getText().toString();
				if (!firstVal.equals(edit.getText().toString())) {
					allMatch = false;
					break;
				}
			}
			if (!allMatch) {
				for (EditText edit : matchItems.get(i))
					edit.setError(matchMsgs.get(i));
				matchItems.get(i)[0].requestFocus();
				return false;
			}
		}

		return true;
	}

	public void clear() {
		atLeastItems.clear();
		atLeastMsgs.clear();

		matchItems.clear();
		matchMsgs.clear();

		mailItems.clear();
		blankItems.clear();
	}

	public void setMailErrorMsg(String mailErrorMsg) {
		this.mailErrorMsg = mailErrorMsg;
	}

	public void setBlankErrorMsg(String blankErrorMsg) {
		this.blankErrorMsg = blankErrorMsg;
	}
}